#include "stdafx.h"

void Button::update_graphical()
{
	if (brush_move)
	{
		delete brush_move;
	}
	if (brush_normal)
	{
		delete brush_normal;
	}
	if (brush_down)
	{
		delete brush_down;
	}
	if (brush_font)
	{
		delete brush_font;
	}
	if (ffm)
	{
		delete ffm;
	}
	if (font)
	{
		delete font;
	}

	brush_move = new Gdiplus::SolidBrush(color_move);
	brush_normal = new Gdiplus::SolidBrush(color_normal);
	brush_down = new Gdiplus::SolidBrush(color_down);
	brush_font = new Gdiplus::SolidBrush(color_font);

	ffm = new Gdiplus::FontFamily(fontname.c_str());
	font = new Gdiplus::Font(ffm, fontsize);
}

void Button::determine_valid_region()
{
	valid_region = new RectShape(left, top, width, height);
}

Button::Button(int left, int top, int width, int height)
{
	//specify the position of the control to be drawn ---------------
	this->left = left;
	this->top = top;
	this->width = width;
	this->height = height;
	//---------------------------------------------------------------

	determine_valid_region();
	update_graphical();
}

Button::~Button()
{
	if (brush_down)
	{
		delete brush_down;
	}
	if (brush_move)
	{
		delete brush_move;
	}
	if (brush_normal)
	{
		delete brush_normal;
	}
	if (brush_font)
	{
		delete brush_font;
	}
	if (ffm)
	{
		delete ffm;
	}
	if (font)
	{
		delete font;
	}

	delete valid_region;
}

void Button::draw(Gdiplus::Graphics & graphics)
{
	//draw the rectangle of the button
	switch (state)
	{
	case STATE_NORMAL:
		graphics.FillRectangle(brush_normal, left, top, width, height);
		break;
	case STATE_MOVE:
		graphics.FillRectangle(brush_move, left, top, width, height);
		break;
	case STATE_DOWN:
		graphics.FillRectangle(brush_down, left, top, width, height);
		break;
	}

	Gdiplus::RectF temp;
	Gdiplus::PointF origin;
	graphics.MeasureString(caption.c_str(), caption.length(), font, Gdiplus::PointF(0, 0), &temp);
	origin.X = (width - temp.Width) / 2 + left;
	origin.Y = (height - temp.Height) / 2 + top;
	graphics.DrawString(caption.c_str(), -1, font, origin, brush_font);
}

bool Button::LButtonDown(int x, int y)
{
	state = STATE_DOWN;

	left_button_down(x, y);
	return true;
}

bool Button::LButtonUp(int x, int y)
{
	state = STATE_MOVE;

	left_button_up(x, y);
	return true;
}

bool Button::MouseMove(int button, int x, int y)
{
	if (state == STATE_NORMAL && button == MOUSE_NONE)
		state = STATE_MOVE;

	mouse_move(button, x, y);
	return true;
}

bool Button::MouseLeave(int button, int x, int y)
{
	state = STATE_NORMAL;

	mouse_leave(button, x, y);
	return true;
}

void Button::set_color_move(Gdiplus::Color color)
{
	color_move = color;
	update_graphical();
}

void Button::set_color_down(Gdiplus::Color color)
{
	color_down = color;
	update_graphical();
}

void Button::set_color_normal(Gdiplus::Color color)
{
	color_normal = color;
	update_graphical();
}

void Button::set_color_text(Gdiplus::Color color)
{
	color_font = color;
	update_graphical();
}

void Button::set_caption(std::wstring caption)
{
	this->caption = caption;
}

void Button::set_fontname(std::wstring name)
{
	fontname = name;
	update_graphical();
}

void Button::set_fontsize(int size)
{
	fontsize = size;
	update_graphical();
}